<template>
  <div>
    <!-- <div class="dashboard-container">
      <div class="dashboard-text">name: {{ name }}</div>
      <el-button @click="ddjjeksxd" type="primary">主要按钮</el-button>
      
    </div> -->
    <audio ref="audioTip" style="display: none" muted="muted">
      <source :src="audio.src" />
    </audio>
    <el-card class="dessqw" shadow="never">
      <div class="wdsnwksker">
        <div>
          <el-input
            @clear="clear"
            clearable
            @keyup.enter.native="dshwnjs"
            class="wsdefggdddddddfs"
            v-model="wsdfdsfdcff.orderSn"
            placeholder="请输入订单编号"
          ></el-input>
        </div>
        <div>
          <el-input
            @clear="clear"
            clearable
            @keyup.enter.native="dshwnjs"
            class="wsdefggdddddddf"
            v-model="wsdfdsfdcff.phone"
            placeholder="请输入手机号码查询"
          ></el-input>
        </div>

        <div class="kwkksdeffbbbggn" @click="dshwnjs">
          <el-button type="primary" icon="el-icon-search">查询订单</el-button>
        </div>
      </div>
      <el-table
        :header-cell-style="{ textAlign: 'center' }"
        :cell-style="{ textAlign: 'center' }"
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column label="序号" type="index" width="50"></el-table-column>
        <el-table-column prop="openId" label="用户openid" width="260">
        </el-table-column>
        <el-table-column prop="orderSn" label="订单编号" width="230">
        </el-table-column>
        <el-table-column prop="phone" label="手机号" width="200">
        </el-table-column>
        <el-table-column prop="remark" label="用户备注"> </el-table-column>
        <el-table-column prop="totalAmount" label="总金额" width="150">
          <template #default="{ row }">
            <span style="color: #ff0000">{{ row.totalAmount }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="addressId" label="用户地址">
          <template slot-scope="scope">
            <span> {{ getAddress(scope.row.addressId).name }}</span>
            <span> {{ getAddress(scope.row.addressId).phone }}</span>
            <br />
            <span> {{ getAddress(scope.row.addressId).address }}</span>
            <br />
            <span> {{ getAddress(scope.row.addressId).addressInfo }}</span>
          </template>
        </el-table-column>
        <el-table-column label="配送方式" prop="status" width="120">
          <template #default="{ row }">
            <el-tag type="success" v-if="row.dispatchType === 1">自提</el-tag>
            <el-tag type="danger" v-else>外卖</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180">
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              @click="handleEdit(scope.$index, scope.row)"
              >查看订单</el-button
            >
            <!-- <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>

      <div class="wdsdw">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[10, 15, 20]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <el-dialog
      @close="close"
      title="订单详情"
      :visible.sync="dialogVisible"
      width="60%"
    >
      <div class="sdwddfvffvssdss">
        <div class="wdaedsvfgvvvxzzz">
          下单人：<span class="yansedd">杜金旗</span>
        </div>
        <div class="wdaedsvfgvvvxzzz">
          联系电话：<span class="yansedd">{{
            wdfddds.phone || "暂无电话"
          }}</span>
        </div>
      </div>
      <div class="wdasdsadfc">
        <div class="wdedssdfff">
          <div class="wddfdfddvcfv">订单号</div>
          <div class="wdeasdeffffff">{{ dfrefddsdfc.orderSn }}</div>
          <div class="wddfdfddvcfv">下单时间</div>
          <div class="wdeasdeffffff">{{ dfrefddsdfc.createTime }}</div>
        </div>
        <div class="wdedssdfff">
          <div class="wddfdfddvcfv" style="height: 70px; line-height: 70px">
            商品名称
          </div>
          <div class="wdeasdeffffff" style="height: 70px; line-height: 70px">
            {{ dfrefddsdfc.productName }}
          </div>
          <div class="wddfdfddvcfv" style="height: 70px; line-height: 70px">
            商品图片
          </div>
          <div class="wdeasdeffffff" style="height: 70px; line-height: 70px">
            <el-image
              class="wedesadseafcf"
              style="width: 50px; height: 50px; line-height: 70px"
              :src="getImge(dfrefddsdfc.productPic)"
            ></el-image>
          </div>
        </div>
        <div class="wdedssdfff">
          <div class="wddfdfddvcfv">商品价格</div>
          <div class="wdeasdeffffff">{{ dfrefddsdfc.productPrice }}</div>
          <div class="wddfdfddvcfv">购买数量</div>
          <div class="wdeasdeffffff">{{ dfrefddsdfc.productQuantity }}</div>
        </div>
        <div class="wdedssdfff">
          <div class="wddfdfddvcfv">商品属性</div>
          <div class="wdeasdeffffff">
            <div
              v-if="wsdsss"
              style="white-space: nowrap"
              class="ewedfvggdfdgfffgf"
            >
              <div
                class="wdwdsdeecvvvvvv"
                v-for="(items, index) in JSON.parse(dfrefddsdfc.productAttr)"
                :key="index"
              >
                {{ items.key }}：
                <el-tag
                  size="small"
                  type="danger"
                  v-for="(items, index) in items.inputValue"
                  :key="index"
                >
                  {{ items }}
                </el-tag>
              </div>
            </div>
            <div v-else>暂无标签</div>
          </div>
          <div class="wddfdfddvcfv">用户备注</div>
          <div class="wdeasdeffffff">{{ wdfddds.remark || "暂无备注" }}</div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Dashboard",
  data() {
    return {
      wsdfdsfdcff: {
        orderSn: "",
        phone: "",
      },
      dfrefddsdfc: {},
      wsdsss: false,
      addressMap: {}, // 地址数据存储对象
      isRefresh: false, // 是否点击刷新页面的标志位
      orders: "desc",
      rsddss: 0,
      dedd: [{ dsd: "无数据" }],
      sortField: "updateTime",
      total: 1,
      page: 1,
      pageSize: 10,
      dialogVisible: false,
      information: "",
      idsex: "",
      form: {
        name: "",
        sort: "",
      },
      currentPage4: 1,
      tableData: [],
      rules: {
        name: [
          { required: true, message: "请选择分类名称！", trigger: "change" },
        ],
      },
      audio: {
        //消息通知
        src: require("../../imgs/yipin/yinps.mp3"),
      },
      wdfddds: {
        phone: "",
        remark: "",
      },
      previousLength: 0, // 上一次数组的长度
    };
  },
  mounted() {
    this.departurEvent();
    // 在组件挂载时开始定时刷新接口
    setInterval(this.departurEvents, 5000);
    window.addEventListener("beforeunload", this.handleUnload);
  },
  beforeDestroy() {
    // 在组件销毁前清除定时任务
    // this.clearTimer();
    window.removeEventListener("beforeunload", this.handleUnload);
  },
  methods: {
    async wdefdgdddsdfxcd() {
      let params = {
        page: this.page,
        pageSize: this.pageSize,
        orderSn: this.wsdfdsfdcff.orderSn,
        phone: this.wsdfdsfdcff.phone,
        // sortField: this.sortField,
        // positiveOrReverse: this.orders,
      };
      const loading = this.$loading({
        lock: true,
        text: "数据努力加载中...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      let res = await this.$API.getOrderList(params);
      if (res.code === 1) {
        loading.close();
        const { data } = res;
        this.total = Number(data.total);
        this.tableData = data.records;
      }
      // console.log(res);
    },
    // 下载地址
    getImge(avatar) {
      // console.log(avatar);
      return "http://192.168.0.6:8080/attachment/download?name=" + avatar;
    },
    // 清除定时任务
    // clearTimer() {
    //   clearInterval(this.timer);
    // },
    ddjjeksxd() {
      this.$nextTick(() => {
        this.$refs.audioTip.play();
      });
      // this.$refs.audioTip.load();
    },
    manualRefresh() {
      // 手动刷新接口数据
      this.isManualRefresh = true; // 设置手动刷新标记为true
      location.reload();
    },
    async departurEvent() {
      let params = {
        page: this.page,
        pageSize: this.pageSize,
        orderSn: this.wsdfdsfdcff.orderSn,
        phone: this.wsdfdsfdcff.phone,
        // sortField: this.sortField,
        // positiveOrReverse: this.orders,
      };
      const loading = this.$loading({
        lock: true,
        text: "数据努力加载中...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      let res = await this.$API.getOrderList(params);
      if (res.code === 1) {
        loading.close();
        const { data } = res;
        this.total = Number(data.total);
        console.log("kkkkkkk");
        //  this.previousLength = data.records.length;
        localStorage.setItem("rsddss", data.total);
        // if()
        data.records.forEach((order) => {
          console.log(order);
          this.dwsdsdsdffddfdf(order.addressId);
        });
        this.tableData = data.records;
      }
      // console.log(res);
    },
    // 获取地址
    async dwsdsdsdffddfdf(addressId) {
      console.log(addressId, "aaaaaaaaaaaaaaaaaa");
      const dusdsd = await this.$API.getAddressList(addressId);
      if (dusdsd.code === 1) {
        this.addressMap = dusdsd.data;
      }
      console.log(dusdsd.data, "地址");
    },
    getAddress() {
      return this.addressMap || "用户进行自提没填写地址";
    },
    async departurEvents() {
      let params = {
        page: this.page,
        pageSize: 9999,
        // sortField: this.sortField,
        // positiveOrReverse: this.orders,
      };
      let res = await this.$API.getOrderList(params);
      // console.log(res, "kkkkkkk");
      if (res.code === 1) {
        const { data } = res;

        this.checkArray(data.total);
        //  console.log( "kkkkkkk");
      }
    },
    handleUnload(event) {
      if (event.ctrlKey) {
        this.isManualRefresh = true; // 设置手动刷新标记为true
      }
    },
    checkArray(total) {
      const value = localStorage.getItem("rsddss");
      //  console.log(value, "8888888888");
      const currentLength = total;
      if (currentLength > value) {
        // console.log("新数据");
        this.$nextTick(() => {
          this.$refs.audioTip.play();
        });
        this.departurEvent();
      } else {
        // console.log("没新数据");
      }
      this.previousLength = currentLength;
      localStorage.setItem("rsddss", this.previousLength);
    },
    handleEdit(index, row) {
      this.idsex = "1";
      this.wsdsss = true;
      if (this.idsex) {
        this.information = "编辑";
      }
      this.wdfddds.phone = row.phone;
      this.wdfddds.remark = row.remark;

      this.dfrefddsdfc.productAttr =
        '[{"key":"","inputValue":["用户无选择属性"]}]';
      // console.log(index, row);
      this.sddwsdrfrfgdsccc(row.id);
      this.dialogVisible = !this.dialogVisible;
      // this.form = { ...row };
    },
    // 订单详细
    async sddwsdrfrfgdsccc(id) {
      const res = await this.$API.getOrderItemList(id);

      if (res.code === 1) {
        console.log(res.data, "详细");
        if (res.data[0].productAttr == undefined) {
          this.$nextTick(() => {
            res.data[0].productAttr =
              '[{"key":"","inputValue":["用户无选择属性"]}]';
            JSON.parse(res.data[0].productAttr);
            this.dfrefddsdfc = res.data[0];
          });

          // this.wsdsss = false;
          console.log(res.data, "详细55");
        } else {
          JSON.parse(res.data[0].productAttr);
          this.dfrefddsdfc = res.data[0];
        }

        //  JSON.parse( this.dfrefddsdfc.productAttr);
        // console.log(this.dfrefddsdfc.productAttr,'----------')
      }
    },
    handleDelete(index, row) {
      // console.log(index, row);
      this.$confirm(
        `此操作将永久删除该【${row.name}】分类名称吗？, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(async () => {
          let res = await this.$API.deleteCategory(row.id);
          // console.log(res);
          this.departurEvent();
          this.$message({
            type: "success",
            message: `删除【${row.name}】分类名称成功!`,
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    onSubmit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (!this.idsex) {
            console.log("新增");
            console.log(this.form);
            // let category = {
            //   name: this.form.name,
            // };
            let res = await this.$API.addCategory(this.form);
            // console.log(res);
            this.dialogVisible = !this.dialogVisible;
            this.departurEvent();
            this.$message({
              type: "success",
              message: `新增分类名称成功!`,
            });
          } else {
            let res = await this.$API.updateCategory(this.form);
            // console.log(res);
            this.dialogVisible = !this.dialogVisible;
            this.departurEvent();
            this.form = {
              name: "",
            };
            // console.log("编辑");
            this.$message({
              type: "success",
              message: `编辑分类成功！`,
            });
          }
        } else {
          // console.log("error submit!!");
          return false;
        }
      });
    },
    wdkswk() {
      this.dialogVisible = !this.dialogVisible;
      this.form = {
        name: "",
      };
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.departurEvent();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.page = val;
      this.departurEvent();
    },
    dshwnjs() {
      this.wdefdgdddsdfxcd();
    },
    clear() {
      this.wsdfdsfdcff = {
        orderSn: "",
        phone: "",
      };
      this.wdefdgdddsdfxcd();
    },
    close() {
      this.idsex = "";
    },
  },
};
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
.dessqw {
  width: 100%;
  padding: 0 20px;
}
.wdssdw {
  width: 350px;
}
.wdsdw {
  text-align: center;
  margin: 0 auto;
  margin-top: 20px;
}
.wdsnwksker {
  display: flex;

  margin: 10px 0;
  width: 700px;
}

.wdasdsadfc {
  margin-top: 20px;
  width: 100%;

  //  border: 3px solid #f3f3f8;
}
.sdwddfvffvssdss {
  display: flex;
  justify-content: space-between;

  padding: 0 10px;
  padding-bottom: 5px;
}
.yansedd {
  color: #4488ff;
  font-size: 16px;
}
.wdaedsvfgvvvxzzz {
  font-size: 18px;
  font-weight: 600;
}
.wddfdfddvcfv {
  width: 15%;
  height: 50px;
  background-color: #f4f4f9;
  border: 2px solid #edeef5;
  text-align: center;
  line-height: 50px;
}
.wdedssdfff {
  display: flex;
  justify-content: space-around;
  align-items: center;
  text-align: center;
}
.wdeasdeffffff {
  width: 35%;
  height: 50px;
  align-items: center;
  border: 2px solid #f3f3f8;
  text-align: center;
  line-height: 50px;
}
.wedesadseafcf {
  display: inline-block;
  text-align: center;
  border-radius: 5px;
  vertical-align: middle;
}
.ewedfvggdfdgfffgf {
  display: flex;
  align-items: center;
  justify-content: center;
}
.wdwdsdeecvvvvvv {
  padding: 0 10px;
}
.wsdefggdddddddf {
  margin-left: 20px;
}
.kwkksdeffbbbggn {
  margin-left: 60px;
}
.wsdefggdddddddfs {
  margin-right: 10px;
}
</style>
<style >
.el-popup-parent--hidden {
  padding-right: 0 !important;
}
</style>
